package com.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.dao.ClassDao;
import com.opensymphony.xwork2.ActionContext;
import com.po.ClassPO;

@Controller
public class ClassAction {
	@Autowired
	private ClassDao cd;
	private List<ClassPO> listClass;
	
	/**
	 * 展示班级的信息
	 * @return
	 */
	public String show(){
		listClass = cd.findClassInfo();
		ActionContext.getContext().put("listClass", listClass);
		return "class_list";
	}
	
	/**
	 * 展示班级详细信息
	 * @return
	 */
	public String showDetail(){
		 //获取班级ID 
		 HttpServletRequest request = ServletActionContext.getRequest();
		 int id = Integer.parseInt(request.getParameter("id"));
		 //给request域中存储班级的id值
		 request.setAttribute("vid", id);
		 //根据班级ID 查找班级的详细的信息
		 ClassPO classPO = new ClassPO();
		 classPO = cd.finClassDetailInfo(id);
		 ActionContext.getContext().put("ClassPO",classPO ); 
		return "class_detail";
	}
	/**
	 * 修改班级详细信息回显数据
	 */
	public String updateDetailEcho(){
		// 获取班级ID
		HttpServletRequest request = ServletActionContext.getRequest();
		int id = Integer.parseInt(request.getParameter("id"));
		int vid = Integer.parseInt(request.getParameter("vid"));
		
		ClassPO classPO = new ClassPO(); 
		classPO = cd.finClassDetailInfo(id);
		//保存班级ID,和详细信息的ID
		request.setAttribute("id", id);
		request.setAttribute("vid", vid);
		
		ActionContext.getContext().put("classPO", classPO);
		return "updateDetailEcho";
	}
	/**
	 * 修改班级详细信息
	 * @return
	 */
	public String updateDetail() {
		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", request.getParameter("id"));
		map.put("begin", request.getParameter("begin"));
		map.put("num", request.getParameter("num"));
		cd.updateClassDetailInfo(map);
		return "class_detail";
	}
	
	/**
	 * 删除班级信息
	 * @return
	 */
	public String delete() {
		// 获取班级ID
		HttpServletRequest req = ServletActionContext.getRequest();
		int id = Integer.parseInt(req.getParameter("id"));
		cd.deleteCourseInfo(id);

		return "classShow";
	}
	
	/**
	 * 修改时回显数据
	 */
	public String updateEcho(){
		// 获取班级ID
		HttpServletRequest request = ServletActionContext.getRequest();
		int id = Integer.parseInt(request.getParameter("id"));
		ClassPO classPO = new ClassPO(); 
		classPO = cd.finClassDetailInfo(id);
		//保存班级ID
		request.setAttribute("id", id);
		ActionContext.getContext().put("classPO", classPO);
		return "updateEcho";
	}
	/**
	 * 修改班级信息
	 * @return
	 */
	public String update() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");

		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		map.put("name",request.getParameter("name"));
		map.put("intro", request.getParameter("intro"));
		cd.updateClassInfo(map);
		return null;
	}

	
}
